package com.spic.system.controller;

import com.spic.common.core.domain.R;
import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.web.page.TableDataInfo;
import com.spic.common.log.annotation.Log;
import com.spic.common.log.enums.BusinessType;
import com.spic.common.security.annotation.PreAuthorize;
import com.spic.system.domain.SysNumberConfig;
import com.spic.system.service.ISysNumberConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 流水号规则配置Controller
 * 
 * @author spic
 * @date 2021-11-02
 */
@RestController
@RequestMapping("/codeConfig")
public class SysNumberConfigController extends BaseController
{
    @Autowired
    private ISysNumberConfigService sysNumberConfigService;

    /**
     * 查询流水号规则配置列表
     */
    @PreAuthorize(hasPermi = "system:codeConfig:list")
    @GetMapping("/list")
    public TableDataInfo list(SysNumberConfig sysNumberConfig)
    {
        startPage();
        List<SysNumberConfig> list = sysNumberConfigService.selectSysNumberConfigList(sysNumberConfig);
        return getDataTable(list);
    }

    /**
     * 导出流水号规则配置列表
     */
    @PreAuthorize(hasPermi = "system:codeConfig:export")
    @Log(title = "流水号规则配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysNumberConfig sysNumberConfig) throws IOException
    {
        List<SysNumberConfig> list = sysNumberConfigService.selectSysNumberConfigList(sysNumberConfig);
        ExcelUtil<SysNumberConfig> util = new ExcelUtil<SysNumberConfig>(SysNumberConfig.class);
        util.exportExcel(response, list, "流水号规则配置数据");
    }

    /**
     * 获取流水号规则配置详细信息
     */
    @PreAuthorize(hasPermi = "system:codeConfig:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysNumberConfigService.selectSysNumberConfigById(id));
    }

    /**
     * 新增流水号规则配置
     */
    @PreAuthorize(hasPermi = "system:codeConfig:add")
    @Log(title = "流水号规则配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysNumberConfig sysNumberConfig)
    {
        if (!sysNumberConfigService.checkCodeNameUnique(sysNumberConfig.getCodename()))
        {
            return AjaxResult.error("新增编码规则'" + sysNumberConfig.getCodename() + "'失败，编码名称已存在");
        }
        return toAjax(sysNumberConfigService.insertSysNumberConfig(sysNumberConfig));
    }

    /**
     * 修改流水号规则配置
     */
    @PreAuthorize(hasPermi = "system:codeConfig:edit")
    @Log(title = "流水号规则配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysNumberConfig sysNumberConfig)
    {
        return toAjax(sysNumberConfigService.updateSysNumberConfig(sysNumberConfig));
    }

    /**
     * 删除流水号规则配置
     */
    @PreAuthorize(hasPermi = "system:codeConfig:remove")
    @Log(title = "流水号规则配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysNumberConfigService.deleteSysNumberConfigByIds(ids));
    }

    /**
     * 获取当前流水号
     */
    @GetMapping("/cur_code")
    public R<String> getCode(@RequestParam String codeName)
    {
        return R.ok(sysNumberConfigService.getCode(codeName));
    }
}
